24687
20230
Wyświetlam listę kontaktów z usługi sieciowej i wyświetlam ją w widoku tableView kontaktów, jak widać na zrzucie ekranu.
Problem polega na tym, że otrzymuję te same wartości znaczników dla pól wyboru pierwszego wiersza w sekcji A, jak również w sekcji S. Posortowałem jedną tablicę i wyświetliłem ją w widoku tabeli indeksowanej. Jak uzyskać różne wartości tagów w zależności od indexPath.row niezależnie od liczby wyświetlanych sekcji. Oto, czego próbowałem
W cellForRowAtIndexPath:
UIButton * checkBox;
if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPhone)
{
checkBox = [[UIButton assign] initWithFrame: CGRectMake (7, 8, 30, 30)];
}
jeszcze
{
checkBox = [[UIButton assign] initWithFrame: CGRectMake (15, 13, 30, 30)];
}
// int cnt = 0;
// for (int i = indexPath.section - 1; i> 0; i--)
// {
// cnt + = [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: i]] count]; // arrayOfCharachters ma znak od „A” do „Z”
//}
//checkBox.tag = cnt + indexPath.row;
[checkBox setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal];
[checkBox addTarget: self action: @selector (checkBoxClicked :) forControlEvents: UIControlEventTouchUpInside];
[checkBox setTag: indexPath.row];
[cell.contentView addSubview: checkBox];
return cell;
}
- (void) checkBoxClicked: (id) sender
{
CGPoint buttonPosition = [nadawca convertPoint: CGPointZero toView: self.tableViewContact];
NSIndexPath * indexPath = [self.tableViewContact indexPathForRowAtPoint: buttonPosition];
UIButton * tappedButton = (UIButton *) nadawca;
NSLog (@ "numer tagu =% d", [tag nadawcy]);
if ([tappedButton.currentImage isEqual: [UIImage imageNamed: @ "checkBox.png"]])
{
[sender setImage: [UIImage imageNamed: @ "checkBoxMarked.png"] forState: UIControlStateNormal];
if (ścieżka_indeksu! = zero)
{
NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row]; // przechowuj identyfikatory pól wyboru w mutableArrayOfIds
NSLog (@ "Tagged check button id =% @", finalIntId);
[arrayOfIds addObject: finalIntId];
}
// NSString * finalIntId = [mutableArrayOfIds objectAtIndex: tappedButton.tag];
// NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row];
}
jeszcze
{
[sender setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal];
NSLog (@ "UnChecked");
// [arrayOfIds removeObjectAtIndex: tappedButton.tag];
}
}
- (NSString *) tableView: (UITableView *) tableView titleForHeaderInSection: (NSInteger) sekcja {
if ([liczba znaków w tablicy] == 0)
{
powrót @"";
}
return [NSString stringWithFormat: @ "% @", [arrayOfCharacters objectAtIndex: sekcja]];
}
- (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
NSArray * toBeReturned = [NSArray arrayWithArray:
[@ "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | # ”
componentsSeparatedByString: @ "|"]];
return toBeReturned;
}
- (NSInteger) tableView: (UITableView *) tableView sectionForSectionIndexTitle: (NSString *) title atIndex: (NSInteger) index
{
NSInteger count = 0;
for (NSString * character in arrayOfCharacters) {
if ([znak jestEqualToString: tytuł]) {
liczba zwrotów;
}
liczyć ++;
}
return 0;
}
- (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView
{
return [liczba znaków w tablicy];
// powrót 1;
}
- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: sekcja (NSInteger)
{
// return [licznik mutableArray];
return [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: sekcja]] count];
} 
ustawiasz tę samą wartość tagu dla wszystkich sekcji, które mają ten sam wiersz.
IndexPath ma dwie właściwości, {section, row}.
Powiedzmy, że sekcja A ma dwa rzędy,
dla row1 -> indexPath.section = 0, indexPath.row = 0;
dla row2-> indexPath.section = 0, indexPath.row = 1;
Powiedzmy, że sekcja S ma dwa rzędy,
dla row1 -> indexPath.section = 1, indexPath.row = 0;
dla row2-> indexPath.section = 1, indexPath.row = 1;
Tak więc dla wiersza 1 sekcji A i wiersza 1 sekcji S ustawiasz tę samą wartość tagu, która wynosi 0. Oto twój problem.
Spróbuj ustawić wartość tagu, jak poniżej.
button.tag = indexPath.section * 1000 + indexPath.row;
podczas pobierania sekcji i rzędu,
NSInteger section = (button.tag) / 1000;
NSInteger row = (button.tag)% 1000;
|
Spróbuj tego...
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
statyczny NSString * CellIdentifier = @ "Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell assign] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
UIButton * checkBox = [[UIButton assign] initWithFrame: CGRectMake (280, 10, 50, 44)];
checkBox.backgroundColor = [UIColor orangeColor];
[checkBox addTarget: self action: @selector (checkBoxClicked: event:) forControlEvents: UIControlEventTouchUpInside];
[checkBox setTag: indexPath.row];
[cell.contentView addSubview: checkBox];
return cell;
}
- (void) checkBoxClicked: (id) sender event: (id) event
{
NSSet * touches = [event allTouches];
UITouch * touch = [dotyka anyObject];
CGPoint currentTouchPosition = [dotknij locationInView: self.tv]; // tutaj tv to obiekt TableView
NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint: currentTouchPosition];
NSLog (@ "wartość indePath.section% d, indexPath.row% d", indexPath.section, indexPath.row);
}
|
Dzieje się tak, ponieważ przypisujesz tag do przycisków NIEZALEŻNYCH sekcji.
Oba pierwsze rzędy sekcji A i S mają wiersz = 0, więc znacznik przypisany do odpowiedniego przycisku ma wartość 0. Powinieneś je przypisać Zachowując odniesienie do swoich sekcji.
Proponuję przypisać wskazówkę dotyczącą dostępności za pomocą formularza oddzielonego przecinkami zawierającego sekcję, wiersz.
I w twojej metodzie
- (void) checkBoxClicked: (id) sender
{
// wybierz ciąg z postaci oddzielonej przecinkami. Pierwsza to Twoja sekcja, druga to rząd.
}
druga opcja to Zrób to, co robisz i zaimplementuj swoją metodę Button w ten sposób.
CGPoint buttonPosition = [nadawca convertPoint: CGPointZero toView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: buttonPosition];
if (ścieżka_indeksu! = nil)
{
... indexpath.section to Twoja sekcja, index path.row to Twój wiersz.
}
Istnieje również trzecia opcja.
w cellforRowAtIndexpath przypisz przyciskowi tytuł
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
[btn setTitle: <# Your Row #> forState: UIControlStateDisabled];
[btn setTag <#Twoja sekcja #>];
więc po otrzymaniu w metodzie przycisku możesz mieć zarówno sekcję (znacznik), jak i wiersz (tytuł dla stanu wyłączonego).
- (void) checkBoxClicked: (id) sender {[button titleForState: UIControlStateDisabled]; // twoja Row
button.tag // twoja sekcja}
|
Spróbuj tego!
W każdej sekcji możesz znać liczbę sekcji, a następnie dodaj liczbę poszczególnych wierszy.
[[fieldTitlesList objectAtIndex: indexPath.section - 1] count] + indexPath.row
Gdzie fieldTitlesList jest tablicą twoich sekcji.
|
Dodałem następujący kod, który rozwiązał mój problem
NSInteger rowNumber = 0;
for (NSInteger i = 0; i